/**
 * Created by lwy on 2017/8/16.
 */

var goodsCategoryLevel;
var page = 1;
var parentCategoryId = $('#twoLEvel').val();
var categoryId = '';
//获取父菜单
$(function(){
    doOneLevel();
    doAjax();
})

function doOneLevel() {
    $.ajax({
        url:'/crm/purchaseOrderC/shopList.json',
        dataType:'json',
        data:{},
        type:'post',
        success:function (data) {
            for(var i=0;i<data.datas.length;i++){
                $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#shopId'))
            }

        }
    })
}

function doTwoLevel() {
    $.ajax({
        url:'/crm/goodsCategoryC/secondLevel.json',
        dataType:'json',
        data:{
            parentCategoryId:$('#faMenu').val(),
        },
        type:'post',
        success:function (data) {
            $('#erjiLEvel').empty();
            $('<option value="">请选择</option>').appendTo($('#erjiLEvel'));
            for(var i=0;i<data.datas.length;i++){
                $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#erjiLEvel'))
            }
            $('#erjiLEvel').change(function () {
                parentCategoryId = $('#erjiLEvel').val();
            })
        }
    })
}

function doAjax(){
    $.ajax({
        url:'/crm/integralOrder/search.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            page:page,
            state:$('#orderStateValue').val(),
            code:$('#code').val()
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                var datas = data.datas;
                for(var i=0;i<datas.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;" id="'+datas[i].id+'">'+datas[i].id+'</td></a>'+
                        '<td class=" ">'+datas[i].code+'</td>'+
                        '<td class=" ">'+datas[i].goodsName+'</td>'+
                        '<td class=" ">'+datas[i].state+'</td>'+
                        '<td class="sorting_1">'+datas[i].name+'</td>'+
                        '<td class="sorting_1">'+datas[i].phone+'</td>'+
                        '<td class="center">'+datas[i].num+'</td>'+
                        '<td class=" ">'+datas[i].payChannel+'</td>'+
                        '<td class=" ">'+datas[i].goodsType+'</td>'+
                        '<td class=" ">'+datas[i].wayBillNum+'</td>'+
                        '<td class=" ">'+datas[i].payIntegral+'</td>'+
                        '<td class=" ">'+datas[i].prizeState+'</td>'+
                        '<td class=" ">'+datas[i].createTime+'</td>'+
                        '<td class=" ">'+datas[i].payTime+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }
                //id处理
                $('.withdrawId').click(function(){
                    var id = $(this).html();
                    window.location.href="/crm/integralOrder/details?id="+id;

                })
                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    pageCount:data.totalPage,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

//查询
$('#refer').click(function(){
    page=1;
    doAjax();
})
var addParentCategoryId = '';
//分类级别选择一级分类，上级分类id隐藏
$('#addType').change(function(){
    if($('#addType').val()==1){
        $('#imageHead').css({'width':'522px','height':'175px'});
        $('#addfaBox,#addcategoryTypeBox').hide();
        $('#cateGoryImgBox').show();
    }else if($('#addType').val()==2){
        doLevel1();
        $('#cateGoryImgBox').hide();
        $('#addcategoryTypeBox').show();
        $('#addfaBox').show();
    }else if($('#addType').val()==3){
        doLevel1();
        if($('#addcategoryType').val()==1){
            $('#imageHead').css({'width':'144px','height':'144px'});
        }else if($('#addcategoryType').val()==2){
            $('#imageHead').css({'width':'186px','height':'120px'});
        }
        $('#imageHead').css({'width':'522px','height':'175px'});
        $('#addcategoryTypeBox').hide();
        $('#addfaBox,#addfaBox2,#cateGoryImgBox').show();
    }
})

function doLevel1() {
    $.ajax({
        url:'/crm/goodsCategoryC/firstLevel.json',
        dataType:'json',
        data:{},
        type:'post',
        success:function (data) {
            for(var i=0;i<data.datas.length;i++){
                $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#addfaMenu'))
            }
            $('#addfaMenu').show();
        }
    })
}
$('#addfaMenu').change(function () {
    addParentCategoryId = $('#addfaMenu').val();
    if($('#addType').val()==3){
        doLevel2();
    }
})
function doLevel2() {
    $.ajax({
        url:'/crm/goodsCategoryC/secondLevel.json',
        dataType:'json',
        data:{
            parentCategoryId:$('#addfaMenu').val(),
        },
        type:'post',
        success:function (data) {
            $('#addfaMenu2').empty();
            $('<option value="">请选择</option>').appendTo($('#addfaMenu2'));
            for(var i=0;i<data.datas.length;i++){
                $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#addfaMenu2'))
            }
            $('#addfaMenu2').change(function () {
                addParentCategoryId = $('#addfaMenu2').val();
            })
        }
    })
}

//添加
$('#addSave').click(function(){
    $.ajax({
        url:'/crm/goodsCategoryC/toAdd.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            name:$('#addName').val(),
            imgUrl:$('#mainImg').attr('src'),
            categoryType:$('#addcategoryType').val(),
            parentCategoryId:addParentCategoryId,
            summary:$('#summary').val(),
            number:$('#number').val(),
            linkUrl:$('#linkUrl').val(),
            sort:$('#number').val(),
            level:$('#addType').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})

//上传主图
var mainImg;
$('#avatarInput').on('change', function(e) {
    var filemaxsize = 1024 * 5;//5M
    var target = $(e.target);
    var Size = target[0].files[0].size / 1024;
    if(Size > filemaxsize) {
        alert('图片过大，请重新选择!');
        $(".avatar-wrapper").childre().remove;
        return false;
    }
    if(!this.files[0].type.match(/image.*/)) {
        alert('请选择正确的图片!')
    } else {
        var filename = document.querySelector("#avatar-name");
        var texts = document.querySelector("#avatarInput").value;
        var teststr = texts; //你这里的路径写错了
        testend = teststr.match(/[^\\]+\.[^\(]+/i); //直接完整文件名的
        filename.innerHTML = testend;
    }

});


$(".avatar-save").on("click", function() {
    var img_lg = document.getElementById('imageHead');
    // 截图小的显示框内的内容
    html2canvas(img_lg, {
        allowTaint: true,
        taintTest: false,
        onrendered: function(canvas) {
            canvas.id = "mycanvas";
            //生成base64图片数据
            var dataUrl = canvas.toDataURL("image/jpeg");
            var newImg = document.createElement("img");
            newImg.src = dataUrl;
            imagesAjax(dataUrl)
        }
    });
})

function imagesAjax(src) {
    var data = {};
    data.img = src;
    data.jid = $('#jid').val();
    mainImg = src;

    $.ajax({
        url:'/crm/image/uploadImage.json',
        data:{
            filed:mainImg,
        },
        dataType:'json',
        type:'post',
        success:function(data){
            if(data.code==1){
                $('#mainImg').show();
                $('#mainImg').attr('src',data.datas.path);
                $('#update').html('更换')
            }
        }
    })
}

// //保存修改
$('#save').click(function(){
    if($('#avatar-name').html()==""){
        alert('请选择图片');
    }else{
        $('#save').attr('data-dismiss','modal')
    }
})


//上传主图
var mainImg2;
$('#avatarInput2').on('change', function(e) {
    var filemaxsize = 1024 * 5;//5M
    var target = $(e.target);
    var Size = target[0].files[0].size / 1024;
    if(Size > filemaxsize) {
        alert('图片过大，请重新选择!');
        $(".avatar-wrapper").childre().remove;
        return false;
    }
    if(!this.files[0].type.match(/image.*/)) {
        alert('请选择正确的图片!')
    } else {
        var filename = document.querySelector("#avatar-name2");
        var texts = document.querySelector("#avatarInput2").value;
        var teststr = texts; //你这里的路径写错了
        testend = teststr.match(/[^\\]+\.[^\(]+/i); //直接完整文件名的
        filename.innerHTML = testend;
    }

});


$(".avatar-save2").on("click", function() {
    var img_lg = document.getElementById('imageHead2');
    // 截图小的显示框内的内容
    html2canvas(img_lg, {
        allowTaint: true,
        taintTest: false,
        onrendered: function(canvas) {
            canvas.id = "mycanvas";
            //生成base64图片数据
            var dataUrl = canvas.toDataURL("image/jpeg");
            var newImg = document.createElement("img");
            newImg.src = dataUrl;
            imagesAjax(dataUrl)
        }
    });
})

function imagesAjax(src) {
    var data = {};
    data.img = src;
    data.jid = $('#jid').val();
    mainImg2 = src;

    $.ajax({
        url:'/crm/image/uploadImage.json',
        data:{
            filed:mainImg2,
        },
        dataType:'json',
        type:'post',
        success:function(data){
            if(data.code==1){
                $('#mainImg2').show();
                $('#mainImg2').attr('src',data.datas.path);
                $('#update2').html('更换')
            }
        }
    })
}


// //保存修改
$('#save2').click(function(){
    if($('#avatar-name2').html()==""){
        alert('请选择图片');
    }else{
        $('#save2').attr('data-dismiss','modal')
    }
})

//编辑分类
$('#editSave').click(function () {
    $.ajax({
        url:'/crm/goodsCategoryC/edit.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            categoryId:categoryId,
            name:$('#editName').val(),
            imgUrl:$('#mainImg2').attr('src'),
            categoryType:$('#editcategoryType').val(),
            summary:$('#editsummary').val(),
            linkUrl:$('#editlinkUrl').val(),
            sort:$('#editNumber').val(),
            isValid:$('#editState').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('修改成功');
                $('#myModal4').hide();
                $('#myModal4').removeClass('fade in');
                $('#myModal4').addClass('fade');
                $('#myModal4').attr('aria-hidden',true);
                $('#myModal4').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal4 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})
